package com.glory.wxelectricitypda.activity;


import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.wxelectricitypda.R;
import com.glory.wxelectricitypda.adapter.ReturnRequestAdapter;
import com.glory.wxelectricitypda.adapter.UnpackagedAdapter;
import com.glory.wxelectricitypda.base.BaseDialogActivity;
import com.glory.wxelectricitypda.bean.BoxLabelReviewBean;
import com.glory.wxelectricitypda.bean.ResponseBean;
import com.glory.wxelectricitypda.bean.ReturnRequestBean;
import com.glory.wxelectricitypda.utils.NetUtils;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;

public class ReturnRequestActivity extends BaseDialogActivity {


    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.etUser)
    FreeUI_ClearEditText etUser;
    @BindView(R.id.mlotId)
    FreeUI_ClearEditText mlotId;
    @BindView(R.id.mlotQTY)
    FreeUI_ClearEditText mlotQTY;
    @BindView(R.id.addData)
    TextView addData;
    @BindView(R.id.returnRecycler)
    RecyclerView returnRecycler;
    private ReturnRequestAdapter mReturnRequestAdapter;


    @Override
    protected int getLayoutResId() {
        return R.layout.activity_return_request;
    }

    @Override
    protected void initView() {
        freeUI_titleView.setTitle("产线退料申请");

        returnRecycler.setLayoutManager(new LinearLayoutManager(ReturnRequestActivity.this));
        returnRecycler.setItemAnimator(new DefaultItemAnimator());
        returnRecycler.addItemDecoration(new DividerItemDecoration(ReturnRequestActivity.this, 1));
        mReturnRequestAdapter = new ReturnRequestAdapter(null);
        mReturnRequestAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mReturnRequestAdapter.isFirstOnly(true);
        returnRecycler.setAdapter(mReturnRequestAdapter);
    }

    @Override
    protected void initData() {

        initEdit(mlotId, s -> {
            if (s.contains("|")) {
                s = s.replace("|", ",").split(",")[2];
            }
            mlotId.setText(s);
        });


        addData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (etUser.getText().toString().isEmpty()) {
                    showToast("请输入工号", false);
                    return;
                }
                if (mlotId.getText().toString().isEmpty()) {
                    showToast("请扫描物料批号", false);
                    return;
                }
                if (mlotQTY.getText().toString().isEmpty()) {
                    showToast("请输入退料数量", false);
                    return;
                }
                requestReturnRequest();
            }
        });

    }

    private void requestReturnRequest() {

        JSONObject map = new JSONObject();
        JSONObject jsonObject = new JSONObject();
        try {
            map.put("MLOT_ID", mlotId.getText().toString());
            map.put("MAIN_QTY", Double.valueOf(mlotQTY.getText().toString()));
            jsonObject.put("Data", map);
            jsonObject.put("MessageName", "EUIT.MLotBackDoc");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtils.requestPostMesForwardUtils(jsonObject, this, new FreeUI_DialogEntityCallBack<ResponseBean<ReturnRequestBean>>(
                new TypeToken<ResponseBean<ReturnRequestBean>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<ResponseBean<ReturnRequestBean>> response) {
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {

                    mReturnRequestAdapter.addData(response.body().getBody().getBACKMLOT());
                    mlotId.setText("");
                    mlotQTY.setText("");
                }
            }

            @Override
            public void onError(Response<ResponseBean<ReturnRequestBean>> response) {
                super.onError(response);
                loadError(response.getException(), "MLotBackDoc");
            }
        });

    }


    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        return new FreeUI_AddViewEntity("header", llMain);
    }

}